** This file examines mechanisms.  
clear


**0. Load data 

use  "${data}\Fines_Year.dta", clear
drop if year >=2019

** forest area in ha according to gfw 2010
gen areastate = 16500000 if state == "BA"
replace areastate = 1530000 if state == "ES"
replace areastate = 17600000 if state == "MG"
replace areastate = 7080000 if state == "PR"
replace areastate = 1770000 if state == "RJ"
replace areastate = 6130000 if state == "SP"
replace areastate = areastate

** population per state, 2010
gen pop_state = 14016906  if state_code == "BA"
replace pop_state = 3514952 if state_code == "ES"
replace pop_state = 19597330 if state_code == "MG"
replace pop_state = 10444526 if state_code == "PR"
replace pop_state = 15989929  if state_code == "RJ"
replace pop_state = 41262199 if state_code == "SP"
replace pop_state = pop_state


*** urbanization rates from here: https://pt.wikipedia.org/wiki/Lista_de_unidades_federativas_do_Brasil_por_taxa_de_urbaniza%C3%A7%C3%A3o, 2010

gen urbrate = .9671 if state_code == "RJ"
replace urbrate = .7207 if state_code == "BA"
replace urbrate = .8529 if state_code == "ES"
replace urbrate = .8338 if state_code == "MG"
replace urbrate = .9588 if state_code == "SP"
replace urbrate = .8531 if state_code == "PR"

bysort state: egen totalfines = sum(n_fined)  
bysort state: egen totalfinesbrl = sum(brl_paid)  
replace totalfinesbrl = totalfinesbrl
bysort state: egen totalcancelled = sum(n_cancelled)  
gen ratio = totalcancelled/totalfines
la var ratio "Cancelled fines/total fines"
gen netfines = totalfines-totalcancelled


gen finesperha = (totalfines/areastate)
gen brlperha = (totalfinesbrl/areastate)
gen finespercapita = totalfines/pop_state
gen brlpercapita = (totalfinesbrl/pop_state)
gen ruralpop = pop_state*(1-urbrate)
gen brlpercapitarur = (totalfinesbrl/(ruralpop))

la var finespercapita "Fines per capita"
la var brlpercapita "Value fines/10,000  people (R)"
la var brlpercapitarur "Fines per rural capita"

la var finesperha "No. fines/1,000 ha"
la var brlperha "Value fines/1,000 ha forest (R)"

encode state_code, gen(stcode)
la define states  1 "Bahia" 2 "Espírito Santo" 3 "Minas Gerais" 4 "Paraná" 5 "Rio de Janeiro" 6 "São Paulo"
la values stcode states

bysort stcode: keep if _n == 1
 
bysort stcode: sum brlperha brlpercapita

gen state_order = 1 if state_code == "BA"
	replace state_order = 2 if state_code == "PR"
	replace state_order = 3 if state_code == "ES"
	replace state_order = 4 if state_code == "RJ"
	replace state_order = 5 if state_code == "SP"
	replace state_order = 6 if state_code == "MG"
	
*2.2 Graphing
preserve
replace brlpercapita = brlpercapita*10000
replace brlperha = brlperha*5000
graph bar brlpercapita brlperha,  fysize(25) over(stcode, sort(state_order) label) bar(1, color("51 166 166")) bar(2, color("242 80 65")) ///
 legend(position(6) rows(1) order(  1 "Value fines/10,000 people (R)" 2 "Value fines/5000 ha forest (R) " )) saving("${figures}\Fines", replace)

restore

graph export "${figures}\Figure4.eps", as(eps) replace	 

gen finesperforestcapita = (totalfinesbrl/100000)/(areastate/ruralpop)


bysort state_code: sum finesperforestcapita


graph bar  finesperforestcapita, asyvars bargap(10) fysize(25) over(stcode, sort(state_order) label) bar(1, color("51 166 166"))  ///
 bar(2, color("0 54 115"))  bar(3, color("41 51 51"))  bar(4, color("242 80 65"))  bar(5, color("140 31 102")) bar(6, color("166 94 51")) /// 
 ytitle("R 100,000/ha/person") saving("${figures}\Fines2", replace) scheme(white_tableau) legend(pos(6) row(1) order(1 4 2 5 6 3  ))

 
 
 
 
 
